<?php
/**
 * 课包列表管理
 * @author a
 *
 */
class Coursepackage extends CT_Controller{
    
    private $offse = 0;
    private $page_size = 0;
    private $page = 0;
    private $size = 10; //每页显示数量
    
    function __construct()
    {
        parent::__construct();
        $this->load->helper("arraytool");   //加载工具
        $this->load->helper("tools_helper");   //加载工具
        $this->t = $this->ct_template;
        
        $this->_nursery_id = 1;     //重置园所ID为1
        
        $this->load->model('course_cate_model');
        $this->load->model('course_list_model');
        $this->load->model('course_package_model');
        $this->load->model('nursery_model');
        
        //分页
        $this->page = $this->input->get('per_page',TRUE) ? intval($this->input->get('per_page',TRUE)) : 1;
        $this->page_size = $this->config->item('per_page')?$this->config->item('per_page'):$this->size;
        $this->offse = ($this->page-1) * $this->page_size;
        
    }
    
    /**
     * 分类列表
     */
    public function Index(){
        try {
            $where = "course_package.status = 1";
            $fild = 'course_cate.id as cc_id,course_cate.title as cc_title,';
            $fild.= 'course_list.id as cl_id,course_list.title as cl_title,nursery.title as nursery_title,';
            $fild.= 'course_package.id,course_package.title,course_package.logo,course_package.intro,course_package.hours,course_package.total_book,';
            $fild.= 'cost_price,sale_price,course_package.weight,type,course_package.create_date,course_package.update_date';
            
            $course_package_list = $this->course_package_model->course_list($where,$fild,'course_package.weight desc',$this->offse,$this->page_size);
            if(empty($course_package_list))throw new Exception('没有记录');
            
            $total = $this->course_package_model->course_list($where,'count(1) as num'); //总数
            $pagelink = $this->pagination($total[0]['num'],$this->page_size);
            
            
            //echo '<pre>';print_r($course_package_list);die();
            
            
        }catch (Exception $e){
            $error = $e->getMessage();
        }
        $this->t->write_view('content', 'admin/coursepackage/index', array(
            'pagelink'     =>  $pagelink,
            'course_package_list'    =>  $course_package_list,
            'error'     =>  empty($error) ? '' : $error,
        ), false);
        $this->t->render();
    }
    /**
     * 修改排序
     */
    public function sort(){
        try {
            $return = array('code'=>10,'msg'=>'修改成功');
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('ID为空');
            $sort = intval($this->input->get('sort'));
            
            $bool = $this->course_package_model->update(array('weight'=>$sort),array('id'=>$id));
            
            if(empty($bool))throw new Exception('修改失败');
            
        }catch (Exception $e){
            $return['code'] = 11;
            $return['msg'] = $e->getMessage();
        }
        echo json_encode($return);
    }
    /**
     * 修改
     */ 
    public function Update(){
        try {
            $id = intval($_REQUEST['id']);
            if(empty($id))throw new Exception('ID为空');
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
                $data = array();
                $data['title'] = $this->input->post('title');
                $data['hours'] = intval($this->input->post('hours'));
                $data['cost_price'] = intval($this->input->post('cost_price'));
                $data['sale_price'] = intval($this->input->post('sale_price'));
                $data['type'] = intval($this->input->post('type'));
                $data['intro'] = $this->input->post('intro');
                $data['course_list_id'] = intval($this->input->post('course_list_id'));
                $data['update_date'] = date('Y-m-d H:i:s');
                $bool = $this->course_package_model->update($data,array('id'=>$id));
                if(empty($bool))throw new Exception('修改失败');
                redirect("/admin/coursepackage");
            }
            
            //获取课包
            $course_package_info = $this->course_package_model->find(array('id'=>$id));
            
            //获取列表ID
            $course_list_info = $this->course_list_model->find(array('id'=>$course_package_info['course_list_id']),'id,course_cate_id');
            
            //获取分类ID
            $course_cate_info = $this->course_cate_model->find(array('id'=>$course_list_info['course_cate_id']),'id,nursery_id');
            
            //获取所有园所
            $nursery_list = $this->nursery_model->findAll(array('status'=>1),'id,title');
            
                        
            $this->t->write_view('content', 'admin/coursepackage/update', array(
                'course_package_info'  =>  $course_package_info,
                'course_cate_info' =>  $course_cate_info,
                'course_list_info'  =>  $course_list_info,
            		'nursery_list' =>  $nursery_list,
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/coursepackage/',
            ), false);
            $this->t->render();  
        }
    }
    /**
     * 添加
     */
    public function Add(){
        try {
            $form_submit = $this->input->post('form_submit');
            if(!empty($form_submit)){
                $data = array();
                $data['title'] = $this->input->post('title');
                $data['hours'] = intval($this->input->post('hours'));
                $data['cost_price'] = intval($this->input->post('cost_price'));
                $data['sale_price'] = intval($this->input->post('sale_price'));
                $data['type'] = intval($this->input->post('type'));
                $data['intro'] = $this->input->post('intro');
                $data['course_list_id'] = intval($this->input->post('course_list_id'));
                $data['create_date'] = date('Y-m-d H:i:s');
                $bool = $this->course_package_model->insert($data);
                if(empty($bool))throw new Exception('添加失败');
                redirect("/admin/coursepackage");
            }
			$nursery_list = $this->nursery_model->findAll(array('status'=>1),'id,title');
            
            $this->t->write_view('content', 'admin/coursepackage/add', array(
                'nursery_list' =>  $nursery_list,
            ), false);
            $this->t->render();
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/coursepackage/',
            ), false);
            $this->t->render(); 
        }
    }
    /**
     * 获取课程分类表
     */
    public function Getcoursecate(){
    	try {
    		$return = array('code'=>10,'msg'=>'成功');
    		$nursery_id = intval($this->input->get('nursery_id'));
    		if(empty($nursery_id))throw new Exception('ID为空');
    
    		$list = $this->course_cate_model->findAll(array('status'=>1,'nursery_id'=>$nursery_id),'id,title');
    
    		if(empty($list))throw new Exception('列表为空');
    
    		$return['list'] = $list;
    
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    /**
     * 获取课程列表
     */
    public function Getcourselist(){
    	try {
    		$return = array('code'=>10,'msg'=>'成功');
    		$course_cate_id = intval($this->input->get('course_cate_id'));
    		if(empty($course_cate_id))throw new Exception('ID为空');
    
    		$list = $this->course_list_model->findAll(array('status'=>1,'course_cate_id'=>$course_cate_id),'id,title');
    
    		if(empty($list))throw new Exception('列表为空');
    
    		$return['list'] = $list;
    
    	}catch (Exception $e){
    		$return['code'] = 11;
    		$return['msg'] = $e->getMessage();
    	}
    	echo json_encode($return);
    }
    /**
     * 删除
     */
    public function Delete(){
        try {
            $id = intval($this->input->get('id'));
            if(empty($id))throw new Exception('ID为空');
        
            $bool = $this->course_package_model->update(array('status'=>0),array('id'=>$id));
            if(empty($bool))throw new Exception('删除失败');
            redirect("/admin/coursepackage");
        }catch (Exception $e){
            $error = $e->getMessage();
            $this->t->write_view('content', 'admin/msg_error', array(
                'error'     =>  $error,
                'time'  =>  10,
                'url'   =>  '/admin/coursepackage/',
            ), false);
            $this->t->render();            
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    
}






























